home *** CD-ROM | disk | FTP | other *** search
- Subject: v08i063: Syntax-checker for *roff
- Newsgroups: mod.sources
- Approved: mirror!rs
-
- Submitted by: Kamal Al-Yahya <kamal@hanauma.STANFORD.EDU>
- Mod.sources: Volume 8, Issue 63
- Archive-name: trmatch
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line,
- # then unpack it by saving it in a file and typing "sh file".
- # If all goes well, you will see the message "End of shell archive."
- # Contents: Makefile trmatch.1 trmatch.c
- PATH=/bin:/usr/bin:/usr/ucb; export PATH
- echo shar: extracting "'Makefile'" '(84 characters)'
- if test -f 'Makefile' ; then
- echo shar: will not over-write existing file "'Makefile'"
- else
- sed 's/^X//' >Makefile <<'@//E*O*F Makefile//'
- X# Makefile for trmatch.
- X
- XCFLAGS = -O
- Xtrmatch:
- X $(CC) $(CFLAGS) -o trmatch trmatch.c
- @//E*O*F Makefile//
- if test 84 -ne "`wc -c <'Makefile'`"; then
- echo shar: error transmitting "'Makefile'" '(should have been 84 characters)'
- fi
- fi # end of overwriting check
- echo shar: extracting "'trmatch.1'" '(841 characters)'
- if test -f 'trmatch.1' ; then
- echo shar: will not over-write existing file "'trmatch.1'"
- else
- sed 's/^X//' >trmatch.1 <<'@//E*O*F trmatch.1//'
- X.TH trmatch L 7/10/86
- X.UC 4
- X.SH NAME
- Xtrmatch \- checks matching in troff documents.
- X.SH SYNOPSIS
- X.B trmatch
- X.I filename
- X.SH DESCRIPTION
- X.I Trmatch
- Xgives error messages if it detects unmatched
- Xbraces, brackets, parentheses, equations, and
- Xdollar signs in troff documents.
- XError messages are sent to the standard error.
- X.br
- XEscaped braces, brackets, parentheses, and dollar signs are not counted.
- XText and equations are checked separately.
- XIt takes equations to be delimited by .EQ and .EN.
- XIf a ``define'' occurs in the equation, the line containing
- Xthe ``define'' is not checked (since it is bound to have unmatches).
- X.SH DIAGNOSTICS
- XDisplayed equations that are started and ended by user-defined
- Xcommands are regarded as text and matching is not checked
- Xseparately.
- X.SH SEE ALSO
- Xtexmatch(L).
- X.SH AUTHOR
- XKamal Al-Yahya, Stanford University
- X
- @//E*O*F trmatch.1//
- if test 841 -ne "`wc -c <'trmatch.1'`"; then
- echo shar: error transmitting "'trmatch.1'" '(should have been 841 characters)'
- fi
- fi # end of overwriting check
- echo shar: extracting "'trmatch.c'" '(8152 characters)'
- if test -f 'trmatch.c' ; then
- echo shar: will not over-write existing file "'trmatch.c'"
- else
- sed 's/^X//' >trmatch.c <<'@//E*O*F trmatch.c//'
- X/*
- X * trmatch: checks matching parantheses, braces, brackets, and dollar signs.
- X * for troff documents
- X *
- X * to compile: cc trmatch.c -lsep -o trmatch
- X */
- X
- Xchar *documentation[] = {
- X" NAME",
- X" trmatch",
- X"",
- X" SYNTAX",
- X" trmatch [parameters] [inputfiles]",
- X"",
- X" parameters:",
- X" in=filename filename is the input file",
- X" (Default: in=stdin)",
- X"",
- X};
- X
- X/* Author: Kamal Al-Yahya 7/20/1986 */
- X
- Xint doclength = { sizeof documentation/sizeof documentation[0] };
- X
- X#include <stdio.h>
- X#include <sys/ioctl.h>
- X#include <sgtty.h>
- X
- Xchar string[80],filename[80];
- Xstruct sgttyb ttystat;
- Xstatic char *name="trmatch";
- Xextern char *strcpy(), *mktemp();
- X
- X/* for getpar */
- Xint xargc;
- Xchar **xargv;
- X
- Xmain(argc,argv)
- Xint argc;
- Xchar *argv[];
- X{
- X FILE *temp;
- X register char *cptr;
- X int piped_in;
- X int i;
- X
- X /* If no arguments, and not in a pipeline, self document */
- X piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
- X if (argc == 1 && !piped_in)
- X {
- X for( i=0; i<doclength; i++)
- X printf("%s\n",documentation[i]);
- X exit (0);
- X }
- X
- X /* process getpar parameters */
- X xargc = argc;
- X xargv = argv;
- X
- X /* first process pipe input */
- X if(piped_in)
- X {
- X troff_match(stdin);
- X fprintf(stderr,"\n");
- X }
- X
- X#ifdef GETPAR
- X /* next process in=inputfiles */
- X if(getpar_("in","s",string))
- X {
- X sscanf(string,"%s",filename);
- X if((temp=fopen(filename,"r")) != NULL)
- X {
- X fprintf(stderr,"%s:\n",filename);
- X troff_match(temp);
- X fprintf(stderr,"\n");
- X fclose(temp);
- X }
- X else
- X fprintf(stderr,"%s: Cannot open %s\n",name,filename);
- X }
- X#endif /* GETPAR */
- X
- X /*
- X * finally process input line for non-getpar arguments and assume
- X * they are also input files
- X */
- X for (xargc--,xargv++; xargc; xargc--,xargv++)
- X {
- X cptr = *xargv;
- X if( *cptr=='-' ) continue; /* this is a flag */
- X while (*cptr)
- X {
- X#ifdef GETPAR
- X if (*cptr == '=') break; /* this is for getpar */
- X#endif /* GETPAR */
- X cptr++;
- X }
- X if (*cptr) continue;
- X cptr = *xargv;
- X if((temp=fopen(cptr,"r")) != NULL)
- X {
- X fprintf(stderr,"%s:\n",cptr);
- X troff_match(temp);
- X fprintf(stderr,"\n");
- X fclose(temp);
- X }
- X else
- X fprintf(stderr,"%s: Cannot open %s\n",name,cptr);
- X }
- X
- X}
- X
- Xtroff_match(fp) /* check matching */
- XFILE *fp;
- X{
- X
- Xint l=1; /* line counter */
- Xint ld=0; /* single left dollar signs */
- Xint rd=0; /* single right dollar signs */
- Xint eq=0; /* eq=1 : equation (delimeted by .EQ and .EN) */
- Xint lp=0; /* left parantheses */
- Xint rp=0; /* right parantheses */
- Xint lb=0; /* left brackets */
- Xint rb=0; /* right brackets */
- Xint lbr=0; /* left braces */
- Xint rbr=0; /* right braces */
- Xint c=' '; /* current character */
- Xint c1=' '; /* previous character */
- Xint lbrl=0; /* line number of left braces */
- Xint lbl=0; /* line number of left bracket */
- Xint lpl=0; /* line number of left parentheses */
- Xint ldl=1; /* line number of left single dollar sign */
- Xint eql=1; /* line number at which equation is started */
- Xint war=0; /* warning status */
- Xint esc=0; /* escape status */
- X
- Xwhile ((c =getc(fp)) != EOF)
- X {
- Xtop:
- X switch(c)
- X {
- X case '\n':
- X l++; /* increment line counter */
- X/* check to see if a single dollar sign is not closed at the same line */
- X if (ld == 1 && war == 0 && c1 != '\\')
- X {
- X fprintf(stderr,"line %d: WARNING: single dollar sign is not closed on the same line\n",l-1);
- X war=1; /* warning has been given */
- X }
- X esc = 0; /* escape status */
- X break;
- X case '{':
- X if (esc == 0)
- X {
- X lbr++;
- X if (lbrl == 0) lbrl=l;
- X }
- X esc = 0; /* escape status */
- X break;
- X case '}':
- X if (esc == 0) rbr++;
- X if (rbr > lbr)
- X {
- X fprintf(stderr,"line %d: unmatched braces\n",l);
- X rbr--; /* reset the count */
- X }
- X if (lbr == rbr) lbrl=0;
- X esc = 0; /* escape status */
- X break;
- X case '[':
- X if (esc == 0)
- X {
- X lb++;
- X if (lbl == 0) lbl=l;
- X }
- X esc = 0; /* escape status */
- X break;
- X case ']':
- X if (esc == 0) rb++;
- X else esc = 0; /* escape status */
- X if (rb > lb)
- X {
- X fprintf(stderr,"line %d: unmatched brackets\n",l);
- X rb--; /* reset the count */
- X }
- X if (lb == rb) lbl=0;
- X esc = 0; /* escape status */
- X break;
- X case '(':
- X if (esc == 0)
- X {
- X lp++;
- X if (lpl == 0) lpl=l;
- X }
- X esc = 0; /* escape status */
- X break;
- X case ')':
- X if (esc == 0) rp++;
- X if (rp > lp)
- X {
- X fprintf(stderr,"line %d: unmatched parentheses\n",l);
- X rp--; /* reset the count */
- X }
- X if (lp == rp) lpl=0;
- X esc = 0; /* escape status */
- X break;
- X case '$':
- X if (esc == 1) /* escaped dollar sign */
- X {
- X c=' '; /* reset the dollar sign */
- X esc = 0; /* escape status */
- X break;
- X }
- X if (ld == 1) rd=1; /* right dollar sign */
- X else
- X {
- X ld=1; /* left dollar sign */
- X ldl=l; /* line number */
- X war=0; /* no warning hs been given */
- X }
- X esc = 0; /* escape status */
- X break;
- X case '.':
- X if (c1 == '\n' || l == 1) /* troff command */
- X {
- X /* see if it is .EQ */
- X c1=getc(fp);
- X if (c1 == '\n')
- X {
- X esc=0;
- X l++;
- X break;
- X }
- X c=getc(fp);
- X if (c == '\n')
- X {
- X esc=0;
- X l++;
- X break;
- X }
- X if (c1 == 'E' && c == 'Q')
- X {
- X if (eq == 1)
- X fprintf(stderr,"line %d: equation started while equation at line %d is still open\n",l,eql);
- X eq=1; /* beginning of equation */
- X eql=l; /* line number */
- X/* Give warning about unclosed openings */
- X if ((lbr-rbr) > 0)
- X fprintf(stderr,"line %d: %d unclosed braces before equation, first brace opened at line %d\n",eql,lbr-rbr,lbrl);
- X if ((lb-rb) > 0)
- X fprintf(stderr,"line %d: %d unclosed brackets before equation, first bracket opened at line %d\n",eql,lb-rb,lbl);
- X if ((lp-rp) > 0)
- X fprintf(stderr,"line %d: %d unclosed parentheses before equation, first parenthesis opened at line %d\n",eql,lp-rp,lpl);
- X/* clear registers */
- X lp=0; lb=0; lbr=0;
- X rp=0; rb=0; rbr=0;
- X lpl=0; lbrl=0; lbl=0;
- X }
- X else if (c1 == 'E' && c == 'N')
- X {
- X if (eq == 0)
- X fprintf(stderr,"line %d: equation ends but no equation beginning\n",l);
- X/* Give warning about unclosed openings */
- X if ((lbr-rbr) > 0)
- X fprintf(stderr,"line %d: %d unclosed braces in equation\n",eql,lbr-rbr);
- X if ((lb-rb) > 0)
- X fprintf(stderr,"line %d: %d unclosed brackets in equation\n",eql,lb-rb);
- X if ((lp-rp) > 0)
- X fprintf(stderr,"line %d: %d unclosed parentheses in equation\n",eql,lp-rp);
- X/* clear registers */
- X lp=0; lb=0; lbr=0;
- X rp=0; rb=0; rbr=0;
- X lpl=0; lbrl=0; lbl=0;
- X eq=0; /* end of equation */
- X }
- X else
- X while ((c = getc(fp)) != EOF)
- X if (c == '\n')
- X {
- X l++;
- X break;
- X }
- X }
- X esc = 0; /* escape status */
- X break;
- X case 'd':
- X/* check for possible define; ignore define lines */
- X esc = 0;
- X if (eq == 1 && c1 == '\n')
- X {
- X if ((c = getc(fp)) == 'e')
- X if ((c = getc(fp)) == 'f')
- X if ((c = getc(fp)) == 'i')
- X if ((c = getc(fp)) == 'n')
- X if ((c = getc(fp)) == 'e')
- X while ((c = getc(fp)) != EOF)
- X if (c == '\n') break;
- X if (c == '\n') l++;
- X }
- X/* if we grapped a character not in the word "define", go to switch
- X to parse the rest of the line */
- X if (!(c=='d'||c=='e'||c=='f'||c=='i'||c=='n'||c=='\n'))
- X goto top;
- X c1 = ' ';
- X esc = 0; /* escape status */
- X break;
- X case '\\':
- X/* check escape status */
- X if (c1 == '\\' && esc == 1) esc = 0;
- X else esc = 1;
- X break;
- X default:
- X esc = 0; /* escape status */
- X break;
- X }
- X c1=c; /* update previous character */
- X if (ld == 1 && rd == 1)
- X {ld=0.; rd=0.;} /* matched dollar signs */
- X }
- Xif ((lbr-rbr) > 0)
- X fprintf(stderr,"file ends: %d unclosed left braces, first opened at line %d \n",lbr-rbr,lbrl);
- Xif ((lb-rb) > 0)
- X fprintf(stderr,"file ends: %d unclosed left brackets, first opened at line %d \n",lb-rb,lbl);
- Xif ((lp-rp) > 0)
- X fprintf(stderr,"file ends: %d unclosed left parentheses, first opened at line %d \n",lp-rp,lpl);
- Xif (ld == 1)
- X fprintf(stderr,"file ends: single dollar sign opened at line %d unclosed\n",ldl);
- Xif (eq == 1)
- X fprintf(stderr,"file ends: equation started at line %d not closed\n",eql);
- X}
- @//E*O*F trmatch.c//
- if test 8152 -ne "`wc -c <'trmatch.c'`"; then
- echo shar: error transmitting "'trmatch.c'" '(should have been 8152 characters)'
- fi
- fi # end of overwriting check
- echo shar: "End of shell archive."
- exit 0
-